package com.example.comm;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.example.comm.Adapter.SectionsPagerAdapter;
import com.example.comm.Bean.User;
import com.example.comm.Fragment.FragmentChat;
import com.example.comm.Fragment.FragmentHome;
import com.example.comm.Fragment.FragmentMine;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener, ViewPager.OnPageChangeListener {
    //private TextView username,nickname;
    private ViewPager2 viewpager;
    private BottomNavigationBar bottomNavigationBar;
    private List<Fragment> fragments;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager=findViewById(R.id.viewpager);
        bottomNavigationBar=findViewById(R.id.bottom);

        initView();

        /*username=findViewById(R.id.username);
        nickname=findViewById(R.id.nickname);
        //用户数据查询
        BmobUser user=BmobUser.getCurrentUser(User.class);
        String id=user.getObjectId();
        BmobQuery<User> myuser =new BmobQuery<User>();
        myuser.getObject(id, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if(e==null){
                    username.setText(user.getUsername());
                    nickname.setText(user.getNickname());
                }else{
                    Toast.makeText(MainActivity.this, "查询失败", Toast.LENGTH_SHORT).show();
                }
            }
        });*/

    }
    //进行两项初始化 ViewPager和navigationBar
    private void initView() {
        initViewPager();
        initBottomNavigation();
    }

    private void initBottomNavigation() {
        //配置底部导航栏
        bottomNavigationBar.setTabSelectedListener(this);
        bottomNavigationBar.clearAll();
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);  //自适应大小
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_DEFAULT);
        bottomNavigationBar.setBarBackgroundColor(R.color.purple_200)
                .setActiveColor(com.ashokvarma.bottomnavigation.R.color.design_default_color_background)
                .setInActiveColor(R.color.black);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.drawable._home,"首页").setInactiveIconResource(R.drawable.home))
                           .addItem(new BottomNavigationItem(R.drawable._search,"论坛").setInactiveIconResource(R.drawable.search))
                           .addItem(new BottomNavigationItem(R.drawable._my,"我的").setInactiveIconResource(R.drawable.my))
                           .setFirstSelectedPosition(0)
                           .initialise();//默认第一个显示面板
    }

    private void initViewPager() {
        //配置ViewPager
        viewpager.setOffscreenPageLimit(3);//缓存
        fragments=new ArrayList<Fragment>();
        fragments.add(new FragmentHome());
        fragments.add(new FragmentChat());
        fragments.add(new FragmentMine());
        SectionsPagerAdapter sectionsPagerAdapter=new SectionsPagerAdapter(this,fragments);
        viewpager.setAdapter(sectionsPagerAdapter);
        //viewpager.addOnPageChangeaListener(this);
        viewpager.setCurrentItem(0);


    }

    @Override
    public void onTabSelected(int position) {

    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {
        viewpager.setCurrentItem(position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        bottomNavigationBar.selectTab(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}